﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Bll.IObjectBlls;
using CSP.Bll;
using CSP.Model.CSPEnum;
using CSP.Model.CSPBBS;
using System.Configuration;
using CSP.Common.Tools;
using CSP.Common.Factories;

namespace CSP.Web.UCenter
{
    public partial class Login : CSPMainPageBase
    {

        private string _urlBack = "";

        /// <summary>
        /// 返回的URL
        /// </summary>
        protected string URLBack
        {
            get {
                return _urlBack;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(Request["urlback"]!=null)
            {
                _urlBack = Server.UrlEncode(Request["urlback"].ToString());
            }
            if (!this.IsPostBack)
            {
                Div_ErrorMessage.Visible = false;
            }
        }

        protected void Btn_Submit_Click(object sender, EventArgs e)
        {
            bool checkValue = CheckFormat();
            if (!checkValue)
            {
                return;
            }
            string userName = this.Txt_UserName.Value.Trim();
            string pwd = this.Txt_Pwd.Value.Trim();
            LoginByUserNameAndPwd(userName, pwd);
        }

        /// <summary>
        /// 验证格式
        /// </summary>
        private bool CheckFormat()
        {
            bool result = true;
            if (Txt_UserName.Value.IndexOf(',') != -1 || Txt_UserName.Value.IndexOf('=') != -1 || Txt_Pwd.Value.IndexOf(',') != -1 &&
                Txt_Pwd.Value.IndexOf('=') != -1)
            {
                Alert("用户名或密码不允许含有特殊字符!");
                result = false;
            }
            else if (Txt_UserName.Value == "" || Txt_Pwd.Value == "")
            {
                Alert("用户名或密码不允许为空!");
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 通过用户名密码登陆
        /// </summary>
        private void LoginByUserNameAndPwd(string userName, string pwd)
        {
            Member member = null;
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            //using (IUserInfoBll userInfoBll = bllFactory.GetUserInfoBll())
            //{
            //    member = userInfoBll.GetUser(userName, pwd);
            //}
            //临时登录
            if (userName == "cxr" && pwd == "123456")
            {
                member = new Member();
                member.Id = 1;
                //member.CommonMember.PassWord = "123456";
            }

            if (member != null)
            {
                Session["userName"] = userName;
                Session["Uid"] = member.Id;
                string url = "/";
                //CreateCookieByMember(member.Id, member.CommonMember.PassWord, DateTime.Now.AddHours(2));
                if (Request["urlback"] != null)
                {
                    url = Request["urlback"].ToString();
                    Response.Redirect(url);
                }
                url = "/MyHome/ServiceManage/Add.aspx";
                SuccessfullRedirect("恭喜您登陆成功!", url);
            }
            else
            {
                LB_ErrorMessage.InnerHtml = "登录名或密码错误";
                Div_ErrorMessage.Visible = true;
            }
        }

        /// <summary>
        /// 创建cookie
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="expires"></param>
        private void CreateCookieByMember(int uid, string pwd, DateTime expires)
        {
            string saltkey = CommonTool.GenerateCode(8);
            string authKey = CommonTool.MD5(ConfigurationManager.AppSettings["AuthKey"].ToString() + saltkey);
            string cookieAuth = CommonTool.DiscuzAuthcodeEncode(pwd + "\t" + uid.ToString(), authKey);

            CookieFactory factory = CookieFactory.GetInstace();
            factory.Expires = expires;
            factory.SetCookie(this, "saltkey", saltkey);
            factory.SetCookie(this, "auth", cookieAuth);
        }

    }
}